<?php

namespace App\Model;

use Illuminate\Support\Facades\DB;

class FinanceBeforeKkWealth extends BaseModel
{
    protected $table = 'finance_room_before_kk_wealth';
    protected $log_table = 'before_kk_log';
    protected $api_log_table = 'finance_before_kk_log';
    protected $connection = 'api';


    public function getList($input)
    {
        $limit = $input['limit'] ?? 10;
        $page = $input['page'] ?? 1;
        $end_date = $input['end_date'] ?? '';
        $room_uid = $input['room_uid'] ??'';
        $where[] = ['already_total','>=','0'];
        if ($end_date) {
            $where['day'] = $end_date;
        }
        if ($room_uid) {
            $where['room_uid'] = $room_uid;
        }
        $data = DB::connection('api')->table($this->table)
            ->leftJoin('room', 'room.room_id', '=', $this->table . '.room_id')
            ->leftJoin('user', 'user.user_id', '=', $this->table . '.user_id')
            ->where($where)
            ->select($this->table . '.*', 'room_uid', 'name', 'uuid', 'nickname')
            ->orderBy('id', 'desc')
            ->get();
        foreach ($data as &$v) {
            $v->all_total = $v->all_total / 100;
            $v->before_total = $v->before_total / 100;
            $v->already_total = $v->already_total / 100;
            $v->surplus_total = $v->surplus_total / 100;
        }
        $total = count($data);
        $res['data'] = $this->objToArray(collect($data)->forPage($page, $limit)->values()->toArray());
        $res['total'] = $total;
        return $res;
    }

    public function getBeforeList($input)
    {
        $limit = $input['limit'] ?? 10;
        $page = $input['page'] ?? 1;
        $end_date = $input['end_date'] ?? '';
        $room_uid = $input['room_uid'] ??'';
        $where[] = ['total','>=','0'];
        if ($end_date) {
            $where['day'] = $end_date;
        }
        if ($room_uid) {
            $where['room_uid'] = $room_uid;
        }
        $data = DB::connection('api')->table($this->api_log_table)
            ->leftJoin('room', 'room.room_id', '=', $this->api_log_table . '.room_id')
            ->leftJoin('user', 'user.user_id', '=',  'room.user_id')
            ->where($where)
            ->select($this->api_log_table . '.*', 'room_uid', 'name', 'uuid', 'nickname')
            ->orderBy('id', 'desc')
            ->get();
        foreach ($data as &$v) {
            $v->total = $v->total / 100;
        }
        $total = count($data);
        $res['data'] = $this->objToArray(collect($data)->forPage($page, $limit)->values()->toArray());
        $res['total'] = $total;
        return $res;
    }

    public function createBeforeLog($data)
    {
        return DB::table($this->log_table)->insert($data);
    }

    public function getLogList($input)
    {

        $limit = $input['limit'] ?? 10;
        $page = $input['page'] ?? 1;
        $where['room_id'] = $input['room_id'];
        $where['day'] = $input['day'];
        $data = DB::table($this->log_table)->where($where)->get();
        $total = count($data);
        $res['data'] = $this->objToArray(collect($data)->forPage($page, $limit)->values()->toArray());
        $res['total'] = $total;
        return $res;
    }


    //获取房间当月预支值
    public function getRoomBeforeWealth($room_id, $month)
    {
        $where['room_id'] = $room_id;
        $whereBetween[] = date('Y-m-01', strtotime($month));
        $whereBetween[] = date('Y-m-t', strtotime($month));
        $res = DB::connection('api')->table($this->api_log_table)
            ->select(DB::raw('SUM(total) as sum_before_total'))
            ->where($where)
            ->whereBetween('day', $whereBetween)
            ->first();
        return $this->objToArray($res);
    }

}
